import longConfig from "../long.config.js";
import {downloadFile} from "./utils/fetchUtils.js";
import {getDirnameByMeterPath, pathJoin} from "./utils/pathUtils.js";
import SpinnerWrapper from "./utils/spinnerUtils.js";

const __dirname = getDirnameByMeterPath(import.meta.url)
const {builderCacheList}  = longConfig

if(builderCacheList && builderCacheList.length > 0){
    // 初始化一个SpinnerWrapper实例
    const customSpinner = new SpinnerWrapper({ text: '准备下载模块...', color: 'cyan' });
    // 使用封装的方法来执行一系列下载任务
    await customSpinner.runTask(async ()=>{
        for(let i = 0; i < builderCacheList.length; i++){
            const {url, name,version} = builderCacheList[i]
            const file = `${name}-${version}`+'.7z'
            const filePath = pathJoin(__dirname, '..', `modules/${file}`);

            try {
                // 对每个下载调用downloadFile，并通过withSpinner包装以显示单独的任务状态
                await customSpinner.withTask(name, async () => {
                    await downloadFile(url, filePath);
                    console.log(`${name}-${version} 下载完成.`);
                });
            } catch (error) {
                console.error(`${name}-${version} 下载失败:`, error);
            }
        }
    })
}
